//
//  MusicPlayHelper.m
//  MusicPlayer
//
//  Created by lanou3g on 15/10/16.
//  Copyright (c) 2015年 laisi. All rights reserved.
//

#import "MusicPlayHelper.h"
#import <AVFoundation/AVFoundation.h>

@interface MusicPlayHelper ()

@property(nonatomic ,strong)AVPlayer* avPlayer;

//将歌曲播放的时间，不断的传给播放列表页面,给timeSlider赋值
@property(nonatomic, strong)NSTimer* timer;

@end

@implementation MusicPlayHelper

#pragma mark --- 获取播放音乐的单例对象
+(instancetype)shareMusicPlayHelper{
    static MusicPlayHelper* helper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[MusicPlayHelper alloc] init];
    });
    return helper;
}

//使用懒加载创建avPlayer对象
-(AVPlayer *)avPlayer{
    if (!_avPlayer) {
        _avPlayer = [[AVPlayer alloc] init];
    }
    return _avPlayer;
}

#pragma mark ---- 根据传过来的MP3url来进行播放
-(void)preparePlayingMusicWithUrlString:(NSString *)urlString{
    //根据URLString创建，avPlayer要播放的item
    AVPlayerItem* item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:urlString]];
    [self.avPlayer replaceCurrentItemWithPlayerItem:item];
    //使用kvo观察avPlayer有没有准备完毕
    [self.avPlayer addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if (self.avPlayer.status == AVPlayerStatusReadyToPlay) {
        [self play];
    }
}

#pragma mark ---- 播放功能
- (void)play{
    if (YES == _isPlaying) {
        return;
    }
    [self.avPlayer play];
    _isPlaying = YES;
    if (_timer) {
        return;
    }
    //歌曲一开始，就创建定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playAction:) userInfo:nil repeats:YES];
}

- (void)playAction:(NSTimer *)timer{
    NSTimeInterval seconds = self.avPlayer.currentTime.value / self.avPlayer.currentTime.timescale;
    if (_delegate && [_delegate respondsToSelector:@selector(playingToTime:)]) {
        [_delegate playingToTime:seconds];
    }
}

#pragma mark ---- 暂停功能
- (void)pause{
    if (_isPlaying == NO) {
        return;
    }
    [self.avPlayer pause];
    _isPlaying = NO;
    //停止定时器
    [_timer invalidate];
    _timer = nil;
}

#pragma mark ---- 根据指定的时间播放歌曲
- (void)seekToPlayWithTime:(NSTimeInterval)time{
//    [self.avPlayer seekToTime:CMTimeMake(time * self.avPlayer.currentTime.timescale, self.avPlayer.currentTime.timescale) completionHandler:^(BOOL finished) {
//        
//    }];
    [self.avPlayer seekToTime:CMTimeMakeWithSeconds(time, self.avPlayer.currentTime.timescale)];
}

#pragma mark ---- 设置音量
-(void)setVolume:(float)volume{
    self.avPlayer.volume = volume;
}

#pragma mark ---- 使用通知观察当前歌曲有没有播放完成
-(instancetype)init{
    self = [super init];
    if (self) {
        //添加通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playDIdEnd) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    }
    return self;
}

-(void)playDIdEnd{
    if (_delegate && [_delegate respondsToSelector:@selector(playingDidEnd)]) {
        [_delegate playingDidEnd];
    }
}













@end
